/*
 * PhonemapSOAPHandler.java.
 */
package com.phonemap.service.server;

import java.util.Iterator;

import javax.xml.bind.JAXB;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SAAJResult;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPMessage;
import javax.xml.transform.dom.DOMSource;

import com.phonemap.service.server.jaxws.DeletePositions;
import com.phonemap.service.server.jaxws.DeleteUser;
import com.phonemap.service.server.jaxws.GetHistory;
import com.phonemap.service.server.jaxws.GetPositions;
import com.phonemap.service.server.jaxws.GetUser;
import com.phonemap.service.server.jaxws.GetUsersByMeter;
import com.phonemap.service.server.jaxws.GetUsersByName;
import com.phonemap.service.server.jaxws.InsertHistory;
import com.phonemap.service.server.jaxws.InsertUser;
import com.phonemap.service.server.jaxws.UpdatePosition;
import com.phonemap.service.server.jaxws.UpdateUser;


// TODO: Auto-generated Javadoc
/**
 * The Class PhonemapSOAPHandler.
 */
public class PhonemapSOAPHandler {

	/** The Constant NAMESPACE_URI. */
	private static final String NAMESPACE_URI = "http://server.service.phonemap.com/";
	
	/** The Constant INSERT_USER_QNAME. */
	private static final QName INSERT_USER_QNAME = new QName(NAMESPACE_URI,
			"insertUser");

	/** The Constant UPDATE_USER_QNAME. */
	private static final QName UPDATE_USER_QNAME = new QName(NAMESPACE_URI,
			"updateUser");

	/** The Constant UPDATE_POSITION_QNAME. */
	private static final QName UPDATE_POSITION_QNAME = new QName(NAMESPACE_URI,
			"updatePosition");

	/** The Constant GET_USER_QNAME. */
	private static final QName GET_USER_QNAME = new QName(NAMESPACE_URI,
			"getUser");

	/** The Constant DELETE_USER_QNAME. */
	private static final QName DELETE_USER_QNAME = new QName(NAMESPACE_URI,
			"deleteUser");

	/** The Constant DELETE_POSITIONS_QNAME. */
	private static final QName DELETE_POSITIONS_QNAME = new QName(
			NAMESPACE_URI, "deletePositions");

	/** The Constant GET_POSITIONS_QNAME. */
	private static final QName GET_POSITIONS_QNAME = new QName(NAMESPACE_URI,
			"getPositions");

	/** The Constant GET_USERS_BY_NAME_QNAME. */
	private static final QName GET_USERS_BY_NAME_QNAME = new QName(
			NAMESPACE_URI, "getUsersByName");

	/** The Constant GET_HISTRY_QNAME. */
	private static final QName GET_HISTORY_QNAME = new QName(NAMESPACE_URI,
			"getHistory");
	
	/** The Constant GET_USERS_BY_METER_QNAME. */
	private static final QName GET_USERS_BY_METER_QNAME = new QName(NAMESPACE_URI,
			"getUsersByMeter");
	
	/** The Constant INSERT_HISTORY_QNAME. */
	private static final QName INSERT_HISTORY_QNAME = new QName(NAMESPACE_URI,
			"insertHistory");
	  

	/** The message factory. */
	private MessageFactory messageFactory;
	
	/** The greeter adapter. */
	private PhonemapAdapter greeterAdapter;

	/**
	 * Instantiates a new phonemap soap handler.
	 *
	 * @throws SOAPException the SOAP exception
	 */
	public PhonemapSOAPHandler() throws SOAPException {
		messageFactory = MessageFactory.newInstance();
		greeterAdapter = new PhonemapAdapter();
	}

	/**
	 * Handle soap request.
	 * 
	 * @param request
	 *            the request
	 * @return the SOAP message
	 * @throws SOAPException
	 *             the SOAP exception
	 */
	@SuppressWarnings("rawtypes")
	public SOAPMessage handleSOAPRequest(SOAPMessage request)
			throws SOAPException {
		SOAPBody soapBody = request.getSOAPBody();
		Iterator iterator = soapBody.getChildElements();
		Object responsePojo = null;
		QName qname = null;
		while (iterator.hasNext()) {
			Object next = iterator.next();
			if (next instanceof SOAPElement) {
				SOAPElement soapElement = (SOAPElement) next;
				qname = soapElement.getElementQName();

				if (INSERT_USER_QNAME.equals(qname)) {
					responsePojo = handleInsertUserRequest(soapElement);
					break;
				} else if (UPDATE_USER_QNAME.equals(qname)) {
					responsePojo = handleUpdateUserRequest(soapElement);
					break;
				} else if (UPDATE_POSITION_QNAME.equals(qname)) {
					responsePojo = handleUpdatePositionRequest(soapElement);
					break;
				} else if (GET_USER_QNAME.equals(qname)) {
					responsePojo = handleGetUserRequest(soapElement);
					break;
				} else if (DELETE_USER_QNAME.equals(qname)) {
					responsePojo = handleDeleteUserRequest(soapElement);
					break;
				} else if (DELETE_POSITIONS_QNAME.equals(qname)) {
					responsePojo = handleDeletePositionsRequest(soapElement);
					break;
				} else if (GET_POSITIONS_QNAME.equals(qname)) {
					responsePojo = handleGetPositionsRequest(soapElement);
					break;
				} else if (GET_USERS_BY_NAME_QNAME.equals(qname)) {
					responsePojo = handleGetUserByNameRequest(soapElement);
					break;
				}  else if (GET_HISTORY_QNAME.equals(qname)) {
					responsePojo = handleGetHistoryRequest(soapElement);
					break;
				} else if (GET_USERS_BY_METER_QNAME.equals(qname)) {
					responsePojo = handlegetUsersByMeterRequest(soapElement);
					break;
				} else if (INSERT_HISTORY_QNAME.equals(qname)) {
					responsePojo = handleinsertHistoryRequest(soapElement);
					break;
				}
			}
		}
		SOAPMessage soapResponse = messageFactory.createMessage();
		soapBody = soapResponse.getSOAPBody();
		if (responsePojo != null) {
			JAXB.marshal(responsePojo, new SAAJResult(soapBody));
		} else {
			SOAPFault fault = soapBody.addFault();
			fault.setFaultString("Unrecognized SOAP request."
					+ qname.toString());
		}
		return soapResponse;
	}
	  
	/**
	 * Handle insert user request.
	 * 
	 * @param soapElement
	 *            the soap element
	 * @return the object
	 */
	private Object handleInsertUserRequest(SOAPElement soapElement) {
		InsertUser insertUserRequest = JAXB.unmarshal(
				new DOMSource(soapElement), InsertUser.class);
		return greeterAdapter.insertUser(insertUserRequest);
	}

	/**
	 * Handle update user request.
	 * 
	 * @param soapElement
	 *            the soap element
	 * @return the object
	 */
	private Object handleUpdateUserRequest(SOAPElement soapElement) {
		UpdateUser updateUserRequest = JAXB.unmarshal(
				new DOMSource(soapElement), UpdateUser.class);
		return greeterAdapter.updateUser(updateUserRequest);
	}

	/**
	 * Handle update position request.
	 * 
	 * @param soapElement
	 *            the soap element
	 * @return the object
	 */
	private Object handleUpdatePositionRequest(SOAPElement soapElement) {
		UpdatePosition updatePositionRequest = JAXB.unmarshal(new DOMSource(
				soapElement), UpdatePosition.class);
		return greeterAdapter.updatePosition(updatePositionRequest);
	}

	/**
	 * Handle get user request.
	 * 
	 * @param soapElement
	 *            the soap element
	 * @return the object
	 */
	private Object handleGetUserRequest(SOAPElement soapElement) {
		GetUser getUserRequest = JAXB.unmarshal(new DOMSource(soapElement),
				GetUser.class);
		return greeterAdapter.getUser(getUserRequest);
	}

	/**
	 * Handle delete user request.
	 * 
	 * @param soapElement
	 *            the soap element
	 * @return the object
	 */
	private Object handleDeleteUserRequest(SOAPElement soapElement) {
		DeleteUser deleteUserRequest = JAXB.unmarshal(
				new DOMSource(soapElement), DeleteUser.class);
		return greeterAdapter.deleteUser(deleteUserRequest);
	}

	/**
	 * Handle delete positions request.
	 * 
	 * @param soapElement
	 *            the soap element
	 * @return the object
	 */
	private Object handleDeletePositionsRequest(SOAPElement soapElement) {
		DeletePositions deletePositionRequest = JAXB.unmarshal(new DOMSource(
				soapElement), DeletePositions.class);
		return greeterAdapter.deletePositions(deletePositionRequest);
	}

	/**
	 * Handle get positions request.
	 * 
	 * @param soapElement
	 *            the soap element
	 * @return the object
	 */
	private Object handleGetPositionsRequest(SOAPElement soapElement) {
		GetPositions getPositionRequest = JAXB.unmarshal(new DOMSource(
				soapElement), GetPositions.class);
		return greeterAdapter.getPositions(getPositionRequest);
	}

	/**
	 * Handle get user by name request.
	 * 
	 * @param soapElement
	 *            the soap element
	 * @return the object
	 */
	private Object handleGetUserByNameRequest(SOAPElement soapElement) {
		GetUsersByName request = JAXB.unmarshal(new DOMSource(soapElement),
				GetUsersByName.class);
		return greeterAdapter.getUsersByName(request);
	}

	/**
	 * Handle get history request.
	 * 
	 * @param soapElement
	 *            the soap element
	 * @return the object
	 */
	private Object handleGetHistoryRequest(SOAPElement soapElement) {
		GetHistory request = JAXB.unmarshal(new DOMSource(soapElement),
				GetHistory.class);
		return greeterAdapter.getHistory(request);
	}
	
	/**
	 * Handleget users by meter request.
	 *
	 * @param soapElement the soap element
	 * @return the object
	 */
	private Object handlegetUsersByMeterRequest(SOAPElement soapElement) {
		GetUsersByMeter request = JAXB.unmarshal(new DOMSource(soapElement),
				GetUsersByMeter.class);
		return greeterAdapter.getUsersByMeter(request);
	}
	
	
	/**
	 * Handleinsert history request.
	 *
	 * @param soapElement the soap element
	 * @return the object
	 */
	private Object handleinsertHistoryRequest(SOAPElement soapElement) {
		InsertHistory request = JAXB.unmarshal(new DOMSource(soapElement),
				InsertHistory.class);
		return greeterAdapter.insertHistory(request);
	}
	  
}
